package com.example.demo.controller;

import com.example.demo.common.ApiResponse;
import com.example.demo.entity.SchoolClass;
import com.example.demo.entity.SchoolGrade;
import com.example.demo.entity.SchoolStudent;
import com.example.demo.service.SchoolService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "学生管理", description = "学生管理")
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Resource
    private SchoolService schoolService;

    @Operation(summary = "多表查询demo", description = "多表查询")
    @GetMapping(value = "getClassAll")
    public Flux<SchoolClass> getClassAll() {
        Flux<SchoolClass> schoolClassFlux = schoolService.findAllSchoolClass();
        return schoolClassFlux;
    }

    @GetMapping(value = "getClassByGradeId")
    public Flux<SchoolClass> getClassByGradeId(Long id) {
        Flux<SchoolClass> schoolClassFlux = schoolService.findSchoolClassByGrade(id);
        return schoolClassFlux;
    }

    @PostMapping(value = "saveClassAndStudent")
    public Mono<ApiResponse> saveClassAndStudent(@RequestBody SchoolClass schoolClass) {
        Mono<String> schoolClassMono = schoolService.saveClassWithStudents(schoolClass);
        return schoolClassMono.thenReturn(ApiResponse.success());
    }

    @GetMapping(value = "getGradeAll")
    public Flux<SchoolGrade> getGradeAll() {
        Flux<SchoolGrade> grade = schoolService.findAllSchoolGrade2();
        return grade;
    }

    @Operation(summary = "多表保存demo", description = "多表保存")
    @PostMapping(value = "saveGrade")
    public Mono<ApiResponse> saveSchoolGrade(@RequestBody SchoolGrade schoolGrade) {
        Mono<SchoolGrade> grade = schoolService.saveSchoolGrade(schoolGrade);
//        return grade;
        return grade.thenReturn(ApiResponse.success("ok"));
    }

    @PostMapping(value = "saveStudent")
    public Mono<ApiResponse> saveStudent(@RequestBody SchoolStudent student) {
        Mono<SchoolStudent> student1 = schoolService.saveSchoolStudent(student);
        return student1.thenReturn(ApiResponse.success("ok"));
    }

    @PostMapping(value = "saveGrades")
    public Mono<ApiResponse> saveSchoolGrades(@RequestBody List<SchoolGrade> schoolGrades) {
        Mono<String> grade = schoolService.saveSchoolGradeList(schoolGrades);
        return grade.thenReturn(ApiResponse.success());
    }

    @PostMapping(value = "saveGradeAll")
    public Mono<ApiResponse> saveSchoolGradeAll(@RequestBody SchoolGrade schoolGrade) {
        Mono<String> grade = schoolService.saveSchoolGradeOne(schoolGrade);
        return grade.thenReturn(ApiResponse.success());
    }

    @PostMapping(value = "deleteSchoolClass")
    public Mono<ApiResponse> deleteSchoolClass(Long id) {
        Mono<String> grade = schoolService.deleteSchoolClassAndStudentsAndCoursesById(id);
        return grade.thenReturn(ApiResponse.success());
    }
}
